{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 04\n",
    "\n",
    "# 矩阵幂\n",
    "Book_4《矩阵力量》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82179f04-01ef-4824-a7ec-80be438ed9fd",
   "metadata": {},
   "source": [
    "该代码演示了在 NumPy 中如何计算矩阵的三次幂与逐元素三次方的区别：\n",
    "\n",
    "1. **矩阵的三次幂**：通过 `matrix_power` 或连续的矩阵乘法 `A @ A @ A` 计算矩阵 $A$ 的三次幂。给定矩阵：\n",
    "\n",
    "   $$\n",
    "   A = \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "   其三次幂 $A^3$ 计算为：\n",
    "\n",
    "   $$\n",
    "   A^3 = A @ A @ A = \\begin{bmatrix} 37 & 54 \\\\ 81 & 118 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "2. **逐元素三次方**：`A ** 3` 计算的是矩阵 $A$ 每个元素的三次方，结果为：\n",
    "\n",
    "   $$\n",
    "   A \\odot A \\odot A = \\begin{bmatrix} 1^3 & 2^3 \\\\ 3^3 & 4^3 \\end{bmatrix} = \\begin{bmatrix} 1 & 8 \\\\ 27 & 64 \\end{bmatrix}\n",
    "   $$\n",
    "\n",
    "代码展示了矩阵幂运算（整体矩阵相乘）和逐元素幂运算（每个元素单独运算）的区别。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a569286-52c7-4d2b-a1c1-57f1b89ba147",
   "metadata": {},
   "source": [
    "## 导入所需库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "826fccd1-ad68-46f8-8229-75da1e8ddec8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from numpy.linalg import matrix_power as pw  # 导入矩阵幂函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0390858-5fb8-4db6-8139-a9c6e98b48ec",
   "metadata": {},
   "source": [
    "## 定义矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b358aa8f-44ad-4366-8fd5-9890ab983691",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([[1., 2.],  # 定义矩阵A\n",
    "              [3., 4.]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ee41f87-83e0-417e-8429-5650e71574c9",
   "metadata": {},
   "source": [
    "## 计算矩阵的三次幂"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dcff939d-82f9-4098-ada3-81a6bf29e76d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 37.,  54.],\n",
       "       [ 81., 118.]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_3 = pw(A, 3)  # 使用matrix_power计算矩阵A的三次幂\n",
    "A_3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fa89d52c-7726-4d10-b170-ddf0274aa160",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 37.,  54.],\n",
       "       [ 81., 118.]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A @ A @ A  # 使用矩阵乘法逐步计算A的三次幂"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3290bcd6-38f4-449f-8dd0-4e51a00a7067",
   "metadata": {},
   "source": [
    "## 元素逐次三次方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9cf3a84f-d907-4e8e-b3e9-c0637c68bd97",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.,  8.],\n",
       "       [27., 64.]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_3_piecewise = A ** 3  # 计算矩阵A的每个元素的三次方\n",
    "A_3_piecewise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
